import {
  add
} from "../../utils/db";
const app = getApp()
//录音器实例
let recorder = app.globalData.recorder;
Page({
  //开始录音
  startRecord() {
    recorder.start({
      format: "mp3"
    })
  },
  //结束录音
  stopRecord() {
    recorder.stop()
    recorder.onStop(res => {
      let tempFilePath = res.tempFilePath
      var nowtime = new Date().getTime()
      wx.cloud.uploadFile({
        cloudPath: nowtime + ".mp3",
        filePath: tempFilePath,
        success: async res => {
          let fileID = res.fileID; //云端地址
          //存入数据库
          var result =await add({
            collection: "record",
            data:{
              src: fileID,
              addtime: nowtime
            }
 
          }).catch(err=>{
            console.error(err)
          })
          wx.showToast({
            title: '成功上传',
          })

        },
        fail: err => {
          console.log(err)
        }
      })
    })
  }

})